
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/user_remark_list_entity.dart';
import 'package:flutter_ncsjqz_getx/util/time.dart';
import 'package:get/get.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';

class ChatRecordLogic extends GetxController {
  TextEditingController textEditingControllerSearch = TextEditingController();

  List<Message> searchList=[];

  int pageIndex=1;

  String? targetId;

  int conversationType = 1;

  /// 搜索
  search(isLoadMore)async{
    searchList=[];
    if(isLoadMore){
      pageIndex++;
    }else{
      pageIndex=1;
      searchList=[];
    }
    RongIMClient.searchMessages(conversationType, targetId!, textEditingControllerSearch.value.text, pageIndex*10, 0, (msgList, code) async {
      List<String> ids=[];
      msgList!.forEach((element) => ids.add(element.targetId));
      if(1==1){
        UserRemarkListEntity chatImageRemarkEntity = await DioService<UserRemarkListEntity>().queryUserRemarkList ({'userIds': ids});
        for(int i=0;i<msgList.length;i++){
          chatImageRemarkEntity.data!.forEach((element) {
            bool isMy=msgList[i].targetId == Constant.userDetailData!.ryUserId;
            Message message=Message(
              cover: isMy ? Constant.userDetailData!.userPic:element.picUrl,
              nickName: isMy ? Constant.userDetailData!.nickName:element.remarkName,
              message: msgList[i].content==null?'':msgList[i].content?.content,
              timer: TimeUtil.convertTime(msgList[i].sentTime),
              targetId: msgList[i].targetId,
            );
            searchList.add(message);
          });
        }
      }else{
        // GroupDetailEntity groupDetailEntity =await Service<GroupDetailEntity>().getUrl(ApiUrl.queryGroupDetail, params: {'groupId':widget.id});//群内成员
        // for(int i=0;i<msgList.length;i++){
        //   bool isMy=msgList[i].targetId==ryUserId;
        //   int length =groupDetailEntity.data.users.length-1;
        //   Message message=Message(
        //     cover:i>length?"":groupDetailEntity.data?.users[i]?.picUrl??"",
        //     nickName: isMy?myName:i>length?"":groupDetailEntity.data?.users[i]?.remarkName??"",
        //     message: msgList[i].content==null?'':msgList[i].content?.content,
        //     timer: TimeUtil.convertTime(msgList[i].sentTime),
        //     targetId: msgList[i].targetId,
        //   );
        //   searchList.add(message);
        // }
      }
      update();
    });
  }

}
class Message{
  String? nickName;
  String? cover;
  String? message;
  String? timer;
  String? targetId;
  Message({this.cover,this.message,this.timer,this.nickName,this.targetId});
}
